<script setup>
import { ref, onMounted, computed } from "vue";
import axios from 'axios';
import { useStore } from 'vuex';

const store = useStore();

// 从 Vuex store 中获取用户数据
const user = computed(() => store.getters.getUser);

// 初始化用户数据
const datainit = () => {
  // 如果需要从 API 获取数据，可以在这里调用
  // 但是目前我们直接从 store 获取数据
  // axios.post('/admin/users/findByPhone', { phone: user.value.phone })
  //   .then(response => {
  //     const userData = response.data.date;
  //     store.dispatch('updateUser', userData);
  //   });
};

onMounted(() => {
  datainit();
});

// 定义 onClickLeft 方法
const onClickLeft = () => {
  history.back();
};

// 注销账户
const deleteuser = () => {
  if (confirm("确定要注销账户吗？")) {
    axios.delete(`/admin/users/delete/${user.value.userId}`)
        .then(response => {
          if (response.data.code === 200) {
            alert("销户成功");
            // 可以在这里添加跳转到登录页或其他操作
          } else {
            alert("销户失败，请重试。");
          }
        })
        .catch(error => {
          console.error("Error deleting user:", error);
          alert("销户失败，请重试。");
        });
  }
};
</script>

<template>
  <van-nav-bar
      title="账号管理"
      left-text=""
      right-text=""
      left-arrow
      @click-left="onClickLeft"
  />
  <div>
    <van-cell-group inset>
      <van-field v-model="user.phone" label="手机号" placeholder="手机号" readonly />
    </van-cell-group>
  </div>
  <div>
    <van-cell-group inset>
      <van-field label="微信号" placeholder="去绑定" readonly />
    </van-cell-group>
  </div>
  <div>
    <van-cell-group inset>
      <van-field label="注销账户" @click="deleteuser" readonly />
    </van-cell-group>
  </div>
</template>

<style scoped>
</style>